﻿Imports System.Net
Imports System.ComponentModel
Imports System.IO

Module modDownload
    Dim dl_progressBar As ProgressBar = Nothing
    Dim dl_Result As en_resultDownload

    Public Function Download_Start(ByVal fileURI As List(Of String), ByVal fileDirectory As String,
                                   Optional ByRef progressBar As ProgressBar = Nothing,
                                   Optional ByVal boolDemonbuddy As Boolean = False) As en_resultDownload

        Dim urlLocation As String
        Dim urlFile As String
        Dim boolError As Boolean
        Dim _hide As Boolean

        If Not (progressBar Is Nothing) Then

            dl_progressBar = progressBar
            If dl_progressBar.Visible = False Then
                _hide = True
                dl_progressBar.Visible = True

            End If
            dl_progressBar.BringToFront()
            dl_progressBar.Value = 0
            dl_progressBar.Minimum = 0
            dl_progressBar.Maximum = fileURI.Count - 1

        End If

        For i As Integer = 0 To (fileURI.Count - 1)

            urlLocation = fileURI.Item(i)

            Using net As New WebClient

                dl_Result = en_resultDownload.dlNothing

                If fileURI.Count = 1 Then

                    dl_progressBar.Maximum = 100
                    AddHandler net.DownloadProgressChanged, AddressOf Download_Progress

                Else

                    dl_progressBar.Maximum = fileURI.Count

                End If
                AddHandler net.DownloadFileCompleted, AddressOf Download_Complete

                If boolDemonbuddy = False Then

                    urlFile = Path.GetFileName(urlLocation)

                Else

                    urlFile = "tempDemonbuddy.zip"

                End If

                If File.Exists(Path.Combine(fileDirectory, urlFile)) Then
                    File.Delete(Path.Combine(fileDirectory, urlFile))
                End If

                net.DownloadFileAsync(New Uri(urlLocation), Path.Combine(fileDirectory, urlFile))

                While dl_Result = en_resultDownload.dlNothing

                    Application.DoEvents()

                End While

            End Using

            If dl_Result = en_resultDownload.dlError Then

                boolError = True

                Exit For

            End If

            If fileURI.Count <> 1 Then dl_progressBar.Value = i

        Next

        If _hide = True Then

            dl_progressBar.Value = 0
            dl_progressBar.Visible = False

        End If

        If boolError = False Then
            Download_Start = en_resultDownload.dlSuccess
        Else
            Download_Start = en_resultDownload.dlError
        End If
    End Function

    Private Sub Download_Progress(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs)

        If Not (dl_progressBar Is Nothing) Then

            dl_progressBar.Value = e.ProgressPercentage

        End If
    End Sub

    Private Sub Download_Complete(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs)

        If (e.Error Is Nothing) Then

            dl_Result = en_resultDownload.dlSuccess

        Else

            dl_Result = en_resultDownload.dlError

        End If
    End Sub
End Module